// 注册
async function register(data) {
    let selectSql = `SELECT * FROM user WHERE userName="${data.userName}" `;
    const selectRows = await global.sql(selectSql)
}

// 添加用户
async function addUser(data) {
    const dateTime = global.commonFun.getNowTime()
    let addSql = `INSERT INTO user(userName,password,phone,createTime, updateTime, isItRegistered) VALUES(?,?,?,?,?,?)`
    let addSqlParams = ['小游侠', data.password, data.phone, dateTime, dateTime, 0];
    const addData = await global.sql(addSql,addSqlParams)
    if (addData.status) {
        let getUserInfo = `SELECT * FROM user WHERE phone="${data.phone}"`
        let userInfoData = await global.sql(getUserInfo)
        let userInfo = userInfoData.data
        delete userInfo[0].password // 删除用户表的密码
        const token = await global.tokenFun.getToken(userInfo[0])
        let addTokenSql = `INSERT INTO token(userId,token,loginTime, updateTime) VALUES(?,?,?,?)`
        const addTokenSqlParams = [userInfo[0].id, token, dateTime, dateTime];
        const addTokenStatus = await global.sql(addTokenSql, addTokenSqlParams)
        if (addTokenStatus?.status) {
            return  {
                code: 200,
                msg: '登录成功',
                data: {
                    ...userInfo[0],
                    token: token,
                }
            }
        }else {
            return  {code: 500, msg: '服务繁忙', data: null}
        }
    }else {
        return  {code: 500, msg: '服务繁忙', data: null}
    }
}

// 查询当前用户存不存在，如果存在就登录
async function checkUser(data) {
    let selectSql = `SELECT * FROM user WHERE phone="${data.phone}" `;
    const selectData = await global.sql(selectSql)
    const selectRows = selectData.data
    if (!selectData?.status ) {
        return  {code: 500, msg: '服务繁忙', data: null}
    }else if (selectRows.length === 0) {
        // 如果没有找到账号，就去创建一个用户
         return await addUser(data)
    }else if (selectRows[0].password === data.password) {
        delete selectRows[0].password
        const dateTime = global.commonFun.getNowTime()
        const token = await global.tokenFun.getToken(selectRows[0],dateTime)
        const delSql = `DELETE FROM token WHERE userId="${selectRows[0].id}"`
        let addSql = `INSERT INTO token(userId,token,loginTime,updateTime) VALUES(?,?,?,?)`
        const addSqlParams = [selectRows[0].id, token, dateTime, dateTime];
        const delStatus = await global.sql(delSql)
        const arr = await Promise.all([global.sql(delSql), global.sql(addSql, addSqlParams)])
        const statusArr = arr.map(item => {
            return item.status
        })
        if (statusArr.includes(false)) {
            return {code: 500, msg: '服务器繁忙', data: null}
        }else {
            return {
                code: 200,
                msg: '登录成功',
                data: {
                    token: token,
                    userInfo: selectRows[0]
                }
            }
        }
    }
}

exports.checkUser = checkUser
exports.register = register
